
if exists(select name from sysobjects where name = 'sp_YKienGetAll2')
	drop proc sp_YKienGetAll2
go
/*
	Created by: Phuoc NH
	Created on: 5/12/2013 (dd/mm/yyyy)
	Description: 
	--

	File:$sql-scripts/Proc.sp_YKienGetAll2.sql
*/

create PROCEDURE sp_YKienGetAll2
      @PageIndex INT = 1
      ,@PageSize INT = 10
      ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY a.ykienid ASC
      )AS RowNumber, a.*, b.MaSinhVien,c.TenLop
     INTO #Results
      FROM  YKien a inner join TraCuuDiem.dbo.SinhVien b on a.NguoiGui=b.MaSinhVien inner join TraCuuDiem.dbo.Lop c on b.MaLop=c.MaLop where a.trangthai<>255 order by a.trangthai asc, NgayGui desc
     
     
           declare @Record int
           set @Record = (SELECT COUNT(*) FROM #Results)
      SELECT *,@Record  AS Record, RowNumber AS RowNumber FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
     
      DROP TABLE #Results
END


